package screen.component
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	import helper.ValueObject;
	
	import screen.CamiScreen;

	public class Tank extends Sprite
	{
		private var _tank:MovieClip;
		private var _tank_shoot_fist:MovieClip;
		private var _tank_shoot_middle:MovieClip;
		private var _tank_shoot_end:MovieClip;
		private var _DURATION:Number = 7;
		private var _MIDDLE_DURATION:Number=1;
		private var _current_point:Point;
		public static var instance:Tank;
		public function Tank()
		{
			if(!instance){
				instance = this;
			}
			init();
		}
		public static function getInstance():Tank{
			if(!instance){
				instance = new Tank()
			}
			return instance;
		}
		private function init():void{
			_tank = ValueObject.C1_RESOURCE.getExportedAsset("Tank") as MovieClip;
			_tank.y = 500;
			_tank.x = (ValueObject.WIDTH - _tank.width)/2;
			addChild(_tank);
			//tweeenTank()
			//setInterval(tweeenTank,_DURATION*1000+1000);
			_tank_shoot_middle = ValueObject.C1_RESOURCE.getExportedAsset("TankShootMiddle") as MovieClip;
			_tank_shoot_middle.visible = false;
			addChild(_tank_shoot_middle);
			
			_tank_shoot_fist = ValueObject.C1_RESOURCE.getExportedAsset("TankShootFirst") as MovieClip;
			addChild(_tank_shoot_fist);
			
			_tank_shoot_end = ValueObject.C1_RESOURCE.getExportedAsset("TankShootEnd") as MovieClip;
			addChild(_tank_shoot_end);
			CamiScreen.getInstance().addEventListener(MouseEvent.CLICK,clickHandle);
			
		}
		private function tweeenTank():void{
			var x_odinal:Number = Math.min(50+ValueObject.WIDTH*Math.random(),ValueObject.WIDTH -50);
			TweenLite.to(_tank,_DURATION,{x:x_odinal});
		}
		public function clickHandle(e:MouseEvent):void{
			if(_tank_shoot_middle.visible||mouseY>_tank.y){
				return;
			}
			/*
			X=(x1y2-x2y1)/(y2-y1);
			Y1 = (x2y1-x1y2)/(x2-x1);
			Y2=(x1y2-x2y1-(y2-y1)*x)*(y1x2-y2x1)/(x1y2-x2y1)/(x2-x1);
			 * */
			var X_odinal:Number = Math.floor((_tank.x*mouseY-mouseX*_tank.y)/(mouseY - _tank.y));
			var Y1_odinal:Number = Math.floor((mouseX*_tank.y-_tank.x*mouseY)/(mouseX -_tank.x));
			var tem1:Number = (_tank.x*mouseY-mouseX*_tank.y)-(mouseY-_tank.y)*ValueObject.WIDTH;
			var tem2:Number = _tank.y*mouseX-mouseY*_tank.x;
			var tem3:Number = (_tank.x*mouseY-mouseX*_tank.y)*(mouseX - _tank.x);
			var Y2_odinal:Number = Math.floor(tem1*tem2/tem3);
			//trace("X_odinal:",X_odinal,"x2:",mouseX,"y2:",mouseY);
			if(X_odinal>=0&&X_odinal<=ValueObject.WIDTH){
				_current_point = new Point(X_odinal,0);
			}else if(Y1_odinal>=0 &&Y1_odinal<=_tank.y){
				_current_point = new Point(0,Y1_odinal)
			}else{
				_current_point = new Point(ValueObject.WIDTH,Y2_odinal);
			}
			shootAirPlane();
		}
		public function shootAirPlane():void{
			_tank_shoot_fist.x = _tank_shoot_middle.x = _tank.x;
			_tank_shoot_fist.y = _tank_shoot_middle.y = _tank.y;
			_tank_shoot_fist.play();
			_tank_shoot_middle.visible = true;
			TweenLite.to(_tank_shoot_middle,_MIDDLE_DURATION,{x:_current_point.x,y:_current_point.y,onUpdate:shootUpdate,onComplete:onComplete});
		}
		private function shootUpdate():void{
			if(OtherAirplane.getInstance().checkShoot(_tank_shoot_middle)){
				onComplete();
				return;
			}
			for(var i:Number = 1 ;i<=3;i++){
				var cami_airplane:CamiAirplane = CamiScreen.getInstance().getChildByName("CAP" + i) as CamiAirplane;
				if(cami_airplane.checkShoot(_tank_shoot_middle)){
					onComplete()
					break;
				}
			}
		}
		private function onComplete():void{
			TweenLite.killTweensOf(_tank_shoot_middle);
			_tank_shoot_middle.visible = false;
			_tank_shoot_end.x = _tank_shoot_middle.x;
			_tank_shoot_end.y = _tank_shoot_middle.y;
			_tank_shoot_end.play();
		}
		
	}
}